package me.xplabs.ui.baseui 
{
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import me.xplabs.Globals;
	import me.xplabs.ui.BaseSprite;
	import me.xplabs.ui.UICompent;
	
	/**
	 * 提示框
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class Alert
	{
		private static var _backGround:Bitmap;
		private static var _ensureBtn:Button;
		private static var _cancelBtn:Button;
		private static var _label:Label;
		private static var _mask:Shape;
		private static var _container:Sprite;
		private static var _isInit:Boolean;
		private static var _type:int;
		
		
		private static var _ensureCall:Function;
		private static var _cancelCall:Function;
		public function Alert() 
		{
			
		}
		private static function init():void
		{
			_mask = new Shape();
			_mask.graphics.beginFill(0x5A5A5A, 0.2);
			_mask.graphics.drawRect(0, 0, Globals.stageW, Globals.stageH);
			_mask.graphics.endFill();
			
			_container = new Sprite();
			//Globals.topLayer.addChild(_container);
			
			_backGround = new Bitmap();
			_backGround.bitmapData = Globals.lib.getResObj(Globals.domain.texture + 26);
			_container.addChild(_backGround);
			
			_ensureBtn = Globals.ui.button("确定", 2);
			_container.addChild(_ensureBtn);
			_ensureBtn.x = 146;
			_ensureBtn.y = 241;
			_ensureBtn.callback = clickEnsureHandler;
			
			_cancelBtn = Globals.ui.button("取消", 2);
			_container.addChild(_cancelBtn);
			_cancelBtn.x = 303;
			_cancelBtn.y = 241;
			_cancelBtn.callback = clickCancelHandler;
			
			_label = Globals.ui.label("什么",290);
			_container.addChild(_label);
			_label.x = 121;
			_label.y = 90;
			_isInit = true;
		}
		
		static private function clickCancelHandler(e:MouseEvent):void 
		{
			if (_cancelCall != null) _cancelCall(e);
			else hide();
		}
		
		static private function clickEnsureHandler(e:MouseEvent):void 
		{
			if (_ensureCall != null) _ensureCall(e);
			else hide();
		}
		/**
		 * 
		 * @param	content 提示框的文本内容
		 * @param	ptype 提示框的类型，0为确定和取消按钮都有，1为只有确定按钮，2为只有取消按钮
		 * @param	ensureCall 点击确定后回调
		 * @param	cancelCall 点击取消后回调
		 */
		public static function show(content:String = "", ptype:int = 0, ensureCall:Function = null, cancelCall:Function = null):void
		{
			if (!_isInit) init();
			if (_type != ptype)
			{
				switch(ptype)
				{
					case 0:
						_ensureBtn.x = 146;
						_cancelBtn.x = 303;
						if(!_ensureBtn.parent) _container.addChild(_ensureBtn);
						if(!_cancelBtn.parent) _container.addChild(_cancelBtn);
						break;
					case 1:
						_ensureBtn.x = _backGround.x + (_backGround.width - _ensureBtn.width) * .5;
						if(!_ensureBtn.parent) _container.addChild(_ensureBtn);
						if(_cancelBtn.parent) _container.removeChild(_cancelBtn);
						break;
					case 2:
						_cancelBtn.x = _backGround.x + (_backGround.width - _cancelBtn.width) * .5;
						if(!_cancelBtn.parent) _container.addChild(_cancelBtn);
						if(_ensureBtn.parent) _container.removeChild(_ensureBtn);
						break;
				}
			}
			_type = ptype;
			_label.text = content;
			if (!_container.parent)
			{
				Globals.topLayer.addChild(_mask);
				Globals.topLayer.addChild(_container);
			}
			_container.x = Globals.centerW - _container.width * .5;
			_container.y = Globals.centerH - _container.height * .5;
		}
		public static function hide():void
		{
			if (_container.parent)
			{
				Globals.topLayer.removeChild(_mask);
				Globals.topLayer.removeChild(_container);
			}
			_ensureCall = null;
			_cancelCall = null;
		}
	}
}